package org.porridge.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;

/**
 * 
 * <p>Title: FileUtil   </p>
 * <p>Description:文件操作工具类</p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: svnkit</p>
 * <p>Create Date/Time: 2014年8月12日 下午8:33:29<p>
 * @author porridge
 * @version 1.0
 * @url www.prdblog.com
 *
 */
public class FileUtil {
	
	/**
	 * 写入文件
	 * @param url
	 * @param filecontent
	 * @return
	 */
	public static boolean setfile(String url,String filecontent,String type){
			 File file = new File(url);
			 return setfile(file,filecontent,type);
	}
	public static boolean setfile(File file,String filecontent){
		 return setfile(file,filecontent,"UTF-8");
	}
	public static boolean setfile(String url,String filecontent){
		 File file = new File(url);
		 return setfile(file,filecontent,"UTF-8");
	}
	public static boolean setfile(File fle,String filecontent,String type){
		try {
			String path=fle.getPath();
			File fs=new File(path.substring(0,path.lastIndexOf(File.separatorChar)));
			if(!fs.exists())
				fs.mkdirs();
			 if(!fle.exists())
				 fle.createNewFile();
			 FileOutputStream fos=new FileOutputStream(fle);
		     OutputStreamWriter osw=new OutputStreamWriter(fos,type);
		     BufferedWriter  bw=new BufferedWriter(osw);
			 bw.write(filecontent);
			 bw.close();
			 osw.close();
			 fos.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	public static String getfile(File file){
		return getfile(file,"UTF-8");
	}
	/**
	 * 读取文件
	 * @param file
	 * @return
	 */
	public static String getfile(File file,String type){
		try{
			BufferedReader buff=new BufferedReader(new InputStreamReader(new FileInputStream(file),type));  
			StringBuilder stb=new StringBuilder();
			while(buff.ready()){
				stb.append(buff.readLine()+"\n");
			}
			buff.close();//关闭文件流使用
			return stb.toString();
		}catch(Exception e){
			return "error";
		}
	}
	/**
	 * 删除某个文档
	 * @param url
	 * @return
	 */
	public static boolean deleFile(String url){
		File file=new File(url);
		boolean bl=false;
		if(file.exists()){
			bl= file.delete();
		}
		return bl;
	}

	
	/**
	 * 移动文件
	 * @param url1  需要转换的文件夹地址
	 * @param url2 转换到的文件夹地下
	 * @param filename  文件名称
	 * @return  返回 true or false
	 */
	public static boolean copydocdel(File file,String url2,String filename){
		boolean bl=false;
		if(file.exists()){
			File fnewpath = new File(url2); 
			if(!fnewpath.exists()) 
				fnewpath.mkdirs(); 
			File fnew = new File(url2+filename); 
			file.renameTo(fnew);
			bl=true;
		}
		return bl;
	}
	
	/**
	 * 复制文件
	 * @param file
	 * @param url2
	 * @param filename
	 * @return
	 */
	public static boolean copydoc(String url1,String url2){
		boolean bl=true;
		 try
        {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(url1);
            if (oldfile.exists() && oldfile.isFile()){ //文件存在时
                InputStream inStream = new FileInputStream(url1); //读入原文件
                File file2=new File(url2);
                if(!new File(url2).exists()){
        			if(!new File(url2).mkdirs())
        				return false;
        		}
                if(file2.exists())
                	file2.delete();
				try {
					file2.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}
                FileOutputStream fs = new FileOutputStream(url2);
                byte[] buffer = new byte[ 1444 ];
                int length;
                while ( ( byteread = inStream.read( buffer ) ) != -1 )
                {
                    bytesum += byteread; //字节数 文件大小
                    fs.write( buffer, 0, byteread );
                }
                fs.close();
                inStream.close();
            }
        }
        catch ( Exception e )
        {
       	 bl=false;
            System.out.println( "复制单个文件操作出错" );
            e.printStackTrace();

        }
		System.out.println(url1+" == "+url2+"----"+bl);
		return bl;
	}
	
	/**
     * 取文后缀名
     * @param filename 文件名
     * @param defExt 默认空
     * @return
     */
	public  static String getExtension(String filename){
		if((filename!=null)&&(filename.length()>0)){
			int i=filename.lastIndexOf(".");
			if((i>-1)&&(i<(filename.length()-1))){
				return filename.substring(i+1);
			}
		}
		return "";
	}
	/**
	 * 去除文件名后缀
	 * @param filename
	 * @return
	 */
	public   static String trimExtension(String filename){
		if((filename!=null)&&(filename.length()>0)){
			int i=filename.lastIndexOf(".");
			if((i>-1)&&(i<(filename.length()-1))){
				return filename.substring(0,i).toLowerCase();
			}
		}
		return filename;
	}
	/**
	 * 格式化文件大小
	 * @param fileS 文件大小
	 * @return
	 */
   public  static String formetFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "K";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "G";
        }
        return fileSizeString;
    }
}
